//_____________________________ Uniform Script End 2nd Update 19/03/18 _____________________________

if (isDedicated || {!isNil "MGI_lastCont"}) exitWith {};

MGI_lastCont = compileFinal "
	params [['_cont',objNull]];
	private '_lastCont'; 
	_invent = (everyContainer _cont - (everyBackpack _cont apply {[typeOf _x,_x]}));
	if !(_invent isEqualTo []) then [{
		_lastCont = _invent select (count _invent -1) select 1;
	},{_lastCont = _cont}];
	_lastCont
";

fnc_otherUnif = {
  params ["_type","_return"];
  if (_type == 0) exitWith {MGI_button = _return select 1; false};
  
  if (_type == 1 && {!isnil "MGI_button"} && {MGI_button == 1}) then {
    disableSerialization;
    _control = _return select 0;
    _index = _return select 1;
    private _idc = ctrlIDC _control;
    private _text = lbText [_idc, _index];
    private _picture = lbPicture [_idc,_index];
    private _uniforms = ("getText (_x >> 'displayName') == _text && {getNumber (_x >> 'ItemInfo' >> 'type') == 801}" configClasses (configFile >> "CfgWeapons"));
    if (count _uniforms > 0) then {
      [_idc,_text,_picture,_index,_uniforms] spawn {
        params ["_idc","_text","_picture","_index","_uniforms","_selectedUnif","_unifContClasses",["_uniformObject",objNull],["_selectedInvent",[]],"_items","_otherItems","_g0"];
        _currentUnif = uniform player;
        _currentInvent = uniformItems player;
        _selectedUnif = ((_uniforms select {toLower (gettext (_x >> "picture")) splitString "\"joinString "\" == _picture}) apply {configName _x}) select 0;
		
        if !(pl_container isKindOf "CAManBase") then [{
			_unifContClasses = (everyContainer pl_container select { (_x select 0) select [0,2] == "U_"});
			_cnt = 0;
			for "_i" from 0 to _index do {
				if (lbText [_idc,_i] == _text) then {
					_cnt = _cnt +1;
					_uniformObject = _unifContClasses select {_selectedUnif == (_x select 0)} select (_cnt -1) select 1
				};
			};
        },{_uniformObject = uniformContainer pl_container}];
        _selectedInvent = [];
        {_selectedInvent pushback _x} foreach (itemCargo _uniformObject);
		if (magazineCargo _uniformObject isEqualType []) then {
			{_selectedInvent pushback _x} foreach (magazineCargo _uniformObject);
		};
        if !(pl_container isKindOf "CAmanbase") then [{
			_items = +itemCargo pl_container;
			_unifItems = _unifContClasses apply {_x select 0};
			_vestContClasses = (everyContainer pl_container select { (_x select 0) select [0,2] == "V_"});
			_vestItems = _vestContClasses apply {_x select 0};
			_otherItems = +_items - _unifItems - _vestItems;
			pl_container setVariable ["allconts",_unifContClasses+_vestContClasses];
			for "_i" from 0 to count (_unifContClasses+_vestContClasses) -1 do {
				if (((pl_container getVariable "allconts") select _i) select 1 == _uniformObject) exitWith {
					(pl_container getVariable "allconts") deleteAt _i
				};
			};
			pl_container setVariable ["allconts", +(pl_container getVariable "allconts") apply {[_x select 0, itemCargo (_x select 1), magazineCargo (_x select 1)]}];
			_g0 = pl_container;
			clearItemCargoGlobal _g0;
        },{
			_g0 = createVehicle ["WeaponHolderSimulated_Scripted", (player modelToWorld [0,1,1]), [], 0, "CAN_COLLIDE"]
        }];
        player forceAddUniform _selectedUnif;
        {player addItemToUniform _x} forEach _selectedInvent;
        call {
			if (pl_container isKindOf "CAManBase") exitWith {removeUniform pl_container};
			{
				_x params ["_cont",["_it",[]],["_mag",[]]];
				pl_container addItemCargoGlobal [_cont,1];
				_createdCont = pl_container call MGI_lastCont;
				{_createdCont addItemCargoGlobal [_x,1];true} count _it;
				{_createdCont addMagazineCargoGlobal [_x,1];true} count _mag;
			} forEach (pl_container getVariable ["allconts",[]]);
			{_g0 addItemCargoGlobal [_x,1]} forEach _otherItems;
        };
        _g0 addItemCargoGlobal [_currentUnif,1];
        _lastCont = _g0 call MGI_lastCont;
        {_lastCont addItemCargoGlobal [_x,1]} forEach _currentInvent;
        if (_g0 != pl_container && {(count itemCargo _g0 + count magazineCargo _g0 + count weaponCargo _g0 + count backpackCargo _g0) == 0}) then {
			deleteVehicle _g0;
        };
      };
    };
  };
  MGI_button = 0; false
};
/////////////////////////////////////////////////////////////////////////
waitUntil {!isNull player};

player addEventHandler ["InventoryOpened", {
	params ["_unit", "_container"]; 
	pl_container = _container; 
	[] spawn { 
		waitUntil {!(isNull findDisplay 602)}; 
		disableSerialization; 
		{(findDisplay 602 displayCtrl _x) ctrlAddEventHandler ["MouseButtonClick", "[0,_this] call fnc_otherUnif"]} forEach [632,640];
		{(findDisplay 602 displayCtrl _x) ctrlAddEventHandler ["LBSelChanged", "[1,_this] spawn fnc_otherUnif"]} forEach [632,640]; 
	}; 
}]; //此事件重生有效
uiSleep 10;
systemChat "添加解除穿衣限制";
//_____________________________ Uniform Script End 2nd Update 19/03/18 _____________________________